package com.veo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.veo.entity.Book;
import com.veo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    //获取首页中的图书数据
    @RequestMapping("/getBookData")
    public String getBookData(Model model, Integer page, Integer category){
        QueryWrapper<Book> queryWrapper = new QueryWrapper<Book>().eq("category", category);
        IPage<Book> iPage = bookService.page(new Page<>(page,4),queryWrapper);
        model.addAttribute("bookList",iPage.getRecords());
        model.addAttribute("pre",iPage.getCurrent() - 1);
        model.addAttribute("next",iPage.getCurrent() + 1);
        model.addAttribute("cur",iPage.getCurrent());
        model.addAttribute("pages",iPage.getPages());
        model.addAttribute("category",category);
        return "bookData";
    }


    /**
     * 图示列表分类详细页面
     */
    @RequestMapping("/bookList")
    public String bookList(String category,Model model){
        model.addAttribute("category",category);
        return "books_list";
    }

    /**
     * 获取图书分类的详细信息
     */
    @RequestMapping("/getBookListData")
    public String getBookListData(String category,Integer page,Integer pageSize,Model model){
        QueryWrapper<Book> queryWrapper = new QueryWrapper<Book>().eq("category", category);
        pageSize = pageSize == null ? 4 : pageSize;
        IPage<Book> iPage = bookService.page(new Page<Book>(page,pageSize),queryWrapper);
        model.addAttribute("bookList",iPage.getRecords());
        model.addAttribute("pre",iPage.getCurrent() - 1);
        model.addAttribute("next",iPage.getCurrent() + 1);
        model.addAttribute("cur",iPage.getCurrent());
        model.addAttribute("pages",iPage.getPages());
        model.addAttribute("category",category);
        model.addAttribute("pageSize",pageSize);
        return "booksListData";
    }

    /**
     * 书的详情页面
     */
    @RequestMapping("/detail")
    public String detail(Integer id,Model model){
        Book book = bookService.getById(id);
        model.addAttribute("book",book);
        return "details";
    }

    /**
     * 图书名称的搜索
     * @return
     */
    @RequestMapping("/search")
    public String search(String bookName,Model model){
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.like("name",bookName);
        List<Book> bookList = bookService.list(wrapper);
        bookList.forEach(System.out::println);
        model.addAttribute("bookList",bookList);
        return "search";
    }
}
